﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FsCarInfo.Core.Entity.IRespositories;
using HMIS.FrameWork.Repository.Nhb;
using FsCarInfo.Core.Entity;
using HMIS.FrameWork.DomainBase;
using HMIS.FrameWork.Repository;

namespace FsCarInfo.Core.Entity.IRespositories.Impl
{
	public class CarRepositoryImpl : RepositoryNhbImpl<Car>, ICarRepository
	{
		#region ICarRepository 成员

		public IList<Car> GetPlistAll(int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			var strFilter = base.GetHqlstrByExtFilter(filters, "c");
			sort = "c." + sort;

			var query = Session.CreateQuery(@"select c from Car as c"
					+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter)
					+ " order by " + sort + " " + dir)
					.SetFirstResult(start)
					.SetMaxResults(limit);

			total = Session.CreateQuery(@"select count(*) from Car as c "
				 + (string.IsNullOrEmpty(strFilter) ? string.Empty : " where " + strFilter))
				.UniqueResult<long>();
			
			return query.List<Car>();
		}

		public bool IsFieldExist(string fieldName, string fieldValue, string id)
		{
			return base.IsFieldExist(fieldName, fieldValue, id, null);
		}

		public IList<Car> QueryPlist(string query, int start, int limit, out long total)
		{
			var q = Session.CreateQuery(@"select s from Car as s
                                                    where lower(s.BrandCode) like lower(:query)
                                                    or lower(s.ModelCode) like lower(:query)
                                                ")
						.SetString("query", "%" + query + "%")
						.SetFirstResult(start)
						.SetMaxResults(limit);

			total = Session.CreateQuery(@"select count(*) from Car as s
                                                    where lower(s.BrandCode) like lower(:query)
                                                    or lower(s.ModelCode) like lower(:query)
                                                    ")
				   .SetString("query", "%" + query + "%")
				   .UniqueResult<long>();

			return q.List<Car>();
		}

		/// <summary>
		/// 扩展下。具备按query查询和filter筛选功能
		/// </summary>
		/// <param name="query"></param>
		/// <param name="start"></param>
		/// <param name="limit"></param>
		/// <param name="sort"></param>
		/// <param name="dir"></param>
		/// <param name="filters"></param>
		/// <param name="total"></param>
		/// <returns></returns>
		public IList<Car> QueryCarList(string query, int start, int limit, string sort, string dir, List<DataFilter> filters, out long total)
		{
			// filter
			var strFilter = base.GetHqlstrByExtFilter(filters, "c");
			// 排序
			string ordStr = string.IsNullOrEmpty(sort) ? string.Empty : " order by c." + sort + " " + dir;
			// sql条件
			string sqlStr = @"from Car as c 
								where (lower(c.IDCardNo) like lower(:query)
									or lower(c.Engin_ID) like lower(:query) 
									or lower(c.Frame_ID) like lower(:query))"
				+ (string.IsNullOrEmpty(strFilter) ? string.Empty : " and " + strFilter);


			var q = Session.CreateQuery(@"select c " + sqlStr + ordStr)
						.SetString("query", "%" + query + "%");
			if (limit > 0)
				q.SetFirstResult(start).SetMaxResults(limit);

			total = Session.CreateQuery(@"select count(*) " + sqlStr)
				.SetString("query", "%" + query + "%")
				.UniqueResult<long>();

			return q.List<Car>();
		}

		#endregion
	}
}
